import request from "@/api/request";
import { download } from "@/api/request/download";

/**
 * 获取数据库表信息
 * @param params 查询参数，包含分页信息和表名列表
 * @returns 数据库表信息
 */
export function getDatabaseTables(params: SearchTableBaseData) {
  return request({
    url: "/generator/databaseTables",
    method: "get",
    params: params,
  });
}

/**
 * 导出数据库表信息
 * @param params 表名数组
 * @returns 导出结果
 */
export function exportDatabaseTables(params: string[]) {
  return request({
    url: "/generator/exportDatabaseTables",
    method: "post",
    data: params,
  });
}

/**
 * 预览代码
 * @param params 表ID
 * @returns 预览结果
 */
export function previewGenerate(params: number | number[]) {
  return request({
    url: "/generator/previewGenerate/" + params.toString(),
    method: "get"
  });
}

/**
 * 下载代码
 * @param params 表ID
 * @returns 下载结果
 */
export function downloadCode(params: number | number[]) {
  download("/generator/downloadCode/" + params.toString(), {}, "podoum.zip", {
    responseType: "blob",
  });
}

/**
 * 获取表信息列表
 * @param params 查询参数，包含分页信息和表名
 * @returns 表信息
 */
export function getList(params: SearchListParams) {
  return request({
    url: "/generator/page",
    method: "get",
    params: params,
  });
}

/**
 * 获取表信息
 * @param params 查询参数，包含表ID
 * @returns 表信息
 */
export function getInfo(params: number) {
  return request({
    url: "/generator/" + params,
    method: "get",
  });
}

/**
 * 修改表信息
 * @param params 表信息
 * @returns 修改结果
 */
export function editInfo(params: DataInfo) {
  return request({
    url: "/generator/update",
    method: "post",
    data: params,
  });
}

/**
 * 删除表信息
 * @param params 表ID
 * @returns 删除结果
 */
export function delteInfo(params: number | number[]) {
  return request({
    url: "/generator/remove/" + params.toString(),
    method: "get",
  });
}

/**
 * 查询表信息实体定义
 */
export interface SearchListParams {
  pageNum: number;
  pageSize: number;
  tableName: string;
  tableDescribe: string;
}

/**
 * 查询数据库表信息实体定义
 */
export interface SearchTableBaseData {
  pageNum: number;
  pageSize: number;
  tableName: string;
}

/**
 * 操作表信息实体定义
 */
export interface DataInfo {
  id: number | null;
  menuParentId: number;
  tableName: string;
  tableDescribe: string;
  className: string;
  tableAuthor: string;
  packageName: string;
  packagePath: string;
  remark: string;
  generatorColumn: columnData[];
}

/**
 * 表字段信息实体定义
 */
export interface columnData {
  id: number;
  tableId: number;
  columnName: string;
  columnDescribe: string;
  columnType: string;
  javaType: string;
  javaField: string;
  isIm: boolean;
  isList: boolean;
  isInsert: boolean;
  isUpdate: boolean;
  isQuery: boolean;
  queryType: string;
  displayType: string;
}
